package com.example.newheze.User;

import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.newheze.LabActivity;
import com.example.newheze.LoginActivity;
import com.example.newheze.MainActivity;
import com.example.newheze.QRActivity;
import com.example.newheze.R;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

public class UserActivity extends Fragment {
    ListViewAdapter listViewAdapter;
    ListView listView;
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_user, container, false);
        listViewAdapter = new ListViewAdapter(this.getActivity(), getData());
        listView = view.findViewById(R.id.listview);
        listView.setAdapter(listViewAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==3){
                    Intent intent=new Intent(getActivity(), LabActivity.class);
                    startActivity(intent);
                }

                else
                    if(position==2)
                    {
                        Intent intent=new Intent(getActivity(), LabActivity.class);
                        intent.putExtra("flag","company");
                        startActivity(intent);
                }else if (position==4){
                    Intent intent = new Intent(getActivity(),QRActivity.class);
                    startActivity(intent);
                    }
                else
                /**
                 * 点击退出，清空本地，返回到登录界面
                 */
                    if (position==5)
                    {
                        SharedPreferences.Editor editor=getActivity().getSharedPreferences("data",MODE_PRIVATE).edit();
                        editor.remove("log_flag");
                        editor.apply();
                        //getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
                        getActivity().finish();
                    }
            }
        });
        return view;
    }
    private List<ListViewModel> getData(){
        List<ListViewModel> list=new ArrayList<ListViewModel>();
        ListViewModel listViewModel=new ListViewModel();
        listViewModel.setUsername("东平用户");
        listViewModel.setUser("用户名");
        list.add(listViewModel);

        //一个对象就是一个列表项，可创建多个
        ListViewModel listViewModel1=new ListViewModel();
        listViewModel1.setUsername("admin");
        listViewModel1.setUser("账号");
        list.add(listViewModel1);

        ListViewModel listViewModel2=new ListViewModel();
        listViewModel2.setUsername("思泉生态农业示范园");
        listViewModel2.setUser("公司");
        list.add(listViewModel2);

        ListViewModel listViewModel3=new ListViewModel();
        listViewModel3.setUsername("大数据实验中心");
        listViewModel3.setUser("开发者");
        list.add(listViewModel3);

        ListViewModel listViewModel4=new ListViewModel();
        listViewModel4.setUsername("二维码分享");
        listViewModel4.setUser("分享");
        list.add(listViewModel4);


        ListViewModel listViewModel5=new ListViewModel();
        listViewModel5.setUsername("退出");
        listViewModel5.setUser("Exit");
        list.add(listViewModel5);


        return list;
    }
}
